package com.sms.plateform.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sms.plateform.common.finals.Constants;
import com.sms.plateform.kernel.entity.UserInfo;

public class CheckSessionOutFilter implements Filter {
	protected FilterConfig filterConfig = null;

	/**
	 * Take this filter out of service.
	 */
	public void destroy() {
		this.filterConfig = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hsrq = (HttpServletRequest) request;
		HttpServletResponse hsrp = (HttpServletResponse) response;
		String reqPage = hsrq.getServletPath();
		if (!reqPage.trim().equals("/login.do")) {
			UserInfo userinfo = (UserInfo) hsrq.getSession().getAttribute(Constants.SESSION_USER);
			if (userinfo == null || "".equals(userinfo.getUsername())) {
				hsrp.sendRedirect("login.do");
				return;
			}
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	

}
